package com.example.myproject.jsontest.compare_performance;
import com.example.myproject.util.JsonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import springfox.documentation.spring.web.json.Json;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/19.
 */
public class Test {
    /**
     * 测试发现gson是jackson 反序列化速度的10倍
     * gson 是5ms,jackson 81ms
     * 序列化是1倍
     * @param args
     */
    public static void main(String[] args){
        //生成较大的json
        List list = new ArrayList();
        for (int i = 0; i < 100; i++) {
            JsonObject obj = new JsonObject();
            obj.setId(i);
            obj.setName("name" + String.valueOf(i));
            list.add(obj);
        }

        //String str = gson.toJson(list);
        Gson gson = new GsonBuilder().create();
        ObjectMapper mapper = new ObjectMapper();
        //String str = JsonUtil.toJson(list);
        long a = System.currentTimeMillis();
        String str = gson.toJson(list);
        System.out.println("ge str end cost="+(System.currentTimeMillis()-a)+",ms");

         a = System.currentTimeMillis();
        String str2 = toJson(mapper,list);
        System.out.println("ge str2 end cost="+(System.currentTimeMillis()-a)+",ms");


        //1,gson解析
        long start1 = System.currentTimeMillis();

        List<JsonObject> l = gson.fromJson(str, new TypeToken<List<JsonObject>>(){}.getType());
        System.out.println("gson time elapse:" + (System.currentTimeMillis() - start1));
        System.out.println(l.size());

        //2,jackson解析

//        long start2 = System.currentTimeMillis();
//        List<JsonObject> l2;
//        l2 = toObject(str,new TypeReference<JsonObject>(){});
//        System.out.println("jackson time elapse:" + (System.currentTimeMillis() - start2));
//        System.out.println(l2.size());

       // 2,jackson解析
//        ObjectMapper mapper = new ObjectMapper();
        long start2 = System.currentTimeMillis();
        List l2 = null;
        try {
//            l2 = mapper.readValue(str, new TypeReference<JsonObject>() {
//            });
            l2 = (List) toObjectByCollectionType(mapper,str,List.class,JsonObject.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("jackson time elapse:" + (System.currentTimeMillis() - start2));
        System.out.println(l2.size());

    }

    public static Object toObjectByCollectionType(ObjectMapper mapper,String json,Class<?> collectionClass, Class<?>... elementClasses){
        try {
            JavaType javaType = mapper.getTypeFactory().constructParametricType(collectionClass,elementClasses);
            return mapper.readValue(json, javaType);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 将对象转换为JSON字符串
    public static String toJson(ObjectMapper mapper,Object object) {
        try {
            return mapper.writeValueAsString(object);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 先反序列化复杂类型 为泛型的Collection Type
     * @param collectionClass
     * @param elementClasses
     * @return
     */

    private static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.getTypeFactory().constructParametricType(collectionClass,elementClasses);
    }

    public static <T> T toObject(String json, TypeReference  typeReference) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(json, typeReference);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
